package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("添加业务：{}", business);
        business.setSubject(2);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    @GetMapping
    public Result ListBusinesses(BusinessDto businessDto) {
        log.info("查询业务：{}", businessDto);
        PageResult<Business> pageResult = businessService.ListBusinesses(businessDto);
        return Result.success(pageResult);
    }

    /*分配商机*/
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机：{}", businessId, userId);
        Business business = new Business();
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        business.setUserId(userId);
        business.setId(businessId);
        businessService.updateById(business);
        return Result.success();
    }

    /*
     * 回显*/
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("回显：{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    /*跟进*/
    @PutMapping
    public Result trackBusiness(@RequestBody Business business) throws Exception {
        log.info("跟进：{}", business);
        businessService.trackBusiness(business);
        return Result.success();
    }

    //踢回公海
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("踢回公海：{}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        businessService.updateById(business);
        return Result.success();
    }
    //商机转客户
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("商机转客户：{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }
    //公海池列表查询
    @GetMapping("/pool")
    public Result ListBusinessPool( BusinessDto businessDto) {
        log.info("公海池列表查询: {}", businessDto);
        PageResult<Business> pageResult = businessService.ListBusinessPool(businessDto);
        return Result.success(pageResult);
    }







}
